package cn.edu.nchu.fragmentdemo.utils;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import cn.edu.nchu.fragmentdemo.R;
import cn.edu.nchu.fragmentdemo.MainActivity;
import cn.edu.nchu.fragmentdemo.entity.MyBasicEntity;
import cn.edu.nchu.fragmentdemo.myComponents.overLays.RouteLay;

import org.osmdroid.views.MapView;
import org.osmdroid.views.overlay.Overlay;

import java.sql.SQLException;
import java.util.HashMap;

public class MyOverlayManager {
    private static Context context;
    private static MapView mapView;
    private MainActivity mainActivity;
    private TextView Overlay_info;
    /**
     * 保存所有的图层
     * message传递，数据保存全部用的实体类
     * 每个实体类有一个id
     * 使用id和overlay来对应
     * 使用原生map.getoverlays.get(Object o)来进行操作
     */
    private static HashMap<String, Overlay> allmyoverlaysHash_Overlay = new HashMap<>();//用来保存所有的图层目标
    private static HashMap<String, MyBasicEntity> allmyoverlaysHash_Entity = new HashMap<>();//用来保存所有的内存指导对象

    /**
     * 分别存储各种类型的数据，marker，polygon，polyline
     * marker下要划分子集，即图集
     * 当前没有其他图集
     */
            /*private static HashMap<String, Overlay> allMyMarkerOverlay = new HashMap<>();
            private static HashMap<String, Overlay> allMyPolyGonOverlay = new HashMap<>();*/
    //所有航线图层
    private static HashMap<String, Overlay> allMyPolyLineOverlay = new HashMap<>();

    /**
     * 最近添加的图层 注释的图层被换了
     */
    //private static MyPolyGoneOverlay nowMyPolyGoneOverlay;
    //private static MyPolylineOverlay nowMyPolylineOverlay;

    //private static AirSpaceLay nowMyPolyGoneOverlay;
    private static RouteLay nowMyPolylineOverlay;

    //swx new 一键规划 返回生成的RouteLay
    public RouteLay getRouteLay() {
        return nowMyPolylineOverlay;
    }


    /**
     * 单例
     */
    private static volatile MyOverlayManager myOverlayManager;

    public static MyOverlayManager getInstance() {
        if (myOverlayManager == null) {
            myOverlayManager = new MyOverlayManager();
        }
        return myOverlayManager;
    }

    public MyOverlayManager() {
        mainActivity = (MainActivity) MainActivity.getActivity();
        context = mainActivity.getApplicationContext();
        mapView = mainActivity.getMap();
        Overlay_info = (TextView) mainActivity.findViewById(R.id.activity_main_overlay);
        setOvery_Info();
    }

    //MyMarkerOverlayGMGPS myMarkerOverlayGuanMiao;
    //MyMarkerOverlay myMarkerOverlay;

    /**
     * 在地图上添加指定的监听图层
     *
     * @param overlaytype
     * @param iconSet
     * @param iconGroup
     * @param icon
     */
            /*public void addMyOverlay(String overlaytype, int iconSet, int iconGroup, int icon) {
                switch (overlaytype) {
                    case "MARKER": {
                        myMarkerOverlay = new MyMarkerOverlay(context, iconSet, iconGroup, icon);
                        //AppDataManager.getInstance().getMyMarkerOverlayStack().push(myMarkerOverlay);
                        mapView.getOverlays().add(myMarkerOverlay);
                        mapView.invalidate();
                        break;
                    }
                    default: {
                        Log.e("addMyOverlay", "not a MARKER");
                    }
                }
            }*/

    /**
     * 在地图上添加指定的监听图层(观瞄模式专用)
     *
     * @param overlaytype
     * @param iconSet
     * @param iconGroup
     * @param icon
     * @param geoPoint
     */
            /*@RequiresApi(api = Build.VERSION_CODES.O)
            public void addMyOverlayGuanMiao(String overlaytype, int iconSet, int iconGroup, int icon, GeoPoint geoPoint) {
                switch (overlaytype) {
                    case "MARKER": {
                        if (AppDataManager.getInstance().isOrNotGuanMiaoObject == true) {
                            myMarkerOverlayGuanMiao = new MyMarkerOverlayGMGPS(context, iconSet, iconGroup, icon, geoPoint);
                            mapView.getOverlays().add(myMarkerOverlayGuanMiao);
                            mapView.invalidate();
                        }
                        if (AppDataManager.getInstance().isOrNotGuanMiaoObject == false) {
                            myMarkerOverlay = new MyMarkerOverlay(context, iconSet, iconGroup, icon);
                            AppDataManager.getInstance().getMyMarkerOverlayStack().push(myMarkerOverlay);
                            mapView.getOverlays().add(myMarkerOverlay);
                            mapView.invalidate();
                        }
                        break;
                    }
                    default: {
                        Log.e("addMyOverlay", "not a MARKER");
                    }
                }
            }*/

    /**
     * 普通图层(POLYGON/POLYLINE)
     */
    public void addMyOverlay(String overlaytype, String markerUK) {
        switch (overlaytype) {
            case "POLYLINE": {
                RouteLay myPolylineOverlay = new RouteLay(markerUK);
                mapView.getOverlays().add(myPolylineOverlay);
                nowMyPolylineOverlay = myPolylineOverlay;
                break;
            }
            /* case "POLYGON": {
             *//* MyPolyGoneOverlay myPolyGoneOverlay = new MyPolyGoneOverlay(context);
                        mapView.getOverlays().add(myPolyGoneOverlay);
                        nowMyPolyGoneOverlay = myPolyGoneOverlay;*//*
                        AirSpaceLay myPolyGoneOverlay = new AirSpaceLay(markerUK);
                        mapView.getOverlays().add(myPolyGoneOverlay);
                        nowMyPolyGoneOverlay = myPolyGoneOverlay;
                        break;
                    }*/
            default: {
                Log.e("addMyOverlay", "not polygon or polyline");
            }
        }
    }

    /**
     * JTAC和PLANE
     */
            /*public void addMyOverlay(String overlaytype, int flag) {
                //getInstance();
                switch (overlaytype) {
                    case "JTAC": {
                        if (flag == 0) {
                            MyMarkerOverlay myMarkerOverlay = new MyMarkerOverlay(context, 0, 0, 0);
                            mapView.getOverlays().add(myMarkerOverlay);
                        }
                        break;
                    }
                    case "PLANEMarker": {
                        if (flag == 0) {
                            MyMarkerOverlay myMarkerOverlay = new MyMarkerOverlay(context, 0, 5, 0);
                            mapView.getOverlays().add(myMarkerOverlay);
                        }
                        break;
                    }
                    default: {
                        Log.e("addMyOverlay", "not JTAC or PLANEMarker");
                    }
                }
            }*/

    /**
     * addOverlay之后添加的是监听图层
     * 在监听图层中创建的实体类，实体类中包含overlay
     * 在实体类创建完成之后，调用此方法，将实体类存储进内存中的图层管理数组
     * 加进图层管理数组之后，自动完成持久化的存储
     */
    public void addToOverlayManager(MyBasicEntity myBasicEntity) {
        String id = myBasicEntity.getId();
        String tag = myBasicEntity.getDbTag();
        String type = myBasicEntity.getPopMenuType();
        Overlay overlay = myBasicEntity.getOverlay();
        if (tag.equals("MARKER") && !type.equals("type5")) {
            //allMyMarkerOverlay.put(id, overlay);
        }
        if (tag.equals("POLYGON")) {
            //allMyPolyGonOverlay.put(id, overlay);
        }
        if (tag.equals("POLYLINE")) {
            allMyPolyLineOverlay.put(id, overlay);
        }
        allmyoverlaysHash_Overlay.put(id, overlay);
        allmyoverlaysHash_Entity.put(id, myBasicEntity);
        //添加进内存之后, 还要进行持久化
        //DbManagerUtil.getInstance().dbStorage(myBasicEntity);
    }

    /**
     * 获取要添加的uid,作为UK存在
     */
    private static String uid;

    public static String getUid() {
        //updateUid();
        uid = UUIDGeneratorUtil.getUUID();
        return uid;
    }

    /**
     * 隐藏图层

     public void hideOverlay(MyBasicEntity myBasicEntity) {
     mapView.getOverlays().remove(myBasicEntity.getOverlay());
     }
     */
    /**
     * 隐藏所有的marker
     * <p>
     * public void hideMarker() {
     * mapView.getOverlays().remove(allMyMarkerOverlay.values());
     * }
     * <p>
     * <p>
     * 隐藏所有polygon
     * <p>
     * public void hidePolygon() {
     * mapView.getOverlays().remove(allMyPolyGonOverlay.values());
     * }
     * <p>
     * <p>
     * 隐藏所有polyline
     */
    public void hidePolyline() {
        mapView.getOverlays().remove(allMyPolyLineOverlay.values());
    }

    /**
     * 删除图层
     */
            /*public void deletOverlay(MyBasicEntity myBasicEntity) throws SQLException {
                mapView.getOverlayManager().remove(myBasicEntity.getOverlay());
                allmyoverlaysHash_Entity.remove(myBasicEntity.getId());
                allmyoverlaysHash_Overlay.remove(myBasicEntity.getId());
                allMyMarkerOverlay.remove(myBasicEntity.getId());
                DbManagerUtil.getInstance().deletMarkerOverlay(myBasicEntity);
            }
*/

    /**
     * 删除：航线的图层
     */
    public void deletPolylineOverlay(MyBasicEntity myBasicEntity) throws SQLException {
        mapView.getOverlayManager().remove(myBasicEntity.getOverlay());
        allmyoverlaysHash_Entity.remove(myBasicEntity.getId());
        allmyoverlaysHash_Overlay.remove(myBasicEntity.getId());
        allMyPolyLineOverlay.remove(myBasicEntity.getId());
        //现在数据库不存这些
        //DbManagerUtil.getInstance().deletPointOverlay(myBasicEntity);
    }

    /**
     * 删除：空域的图层
     */
            /*public void deletPolygonOverlay(MyBasicEntity myBasicEntity) throws SQLException {
                mapView.getOverlayManager().remove(myBasicEntity.getOverlay());
                allmyoverlaysHash_Entity.remove(myBasicEntity.getId());
                allmyoverlaysHash_Overlay.remove(myBasicEntity.getId());
                allMyPolyGonOverlay.remove(myBasicEntity.getId());
                //现在数据库不存这些
                DbManagerUtil.getInstance().deletAirPointOverlay(myBasicEntity);
            }*/

    /**
     * 移除最近添加的MyOverlay
     * 用于polyline和polygon的结束绘制
     */
    public void removeMyOverlay(String type) {
        if (type.equals("POLYLINE")) {
            if (nowMyPolylineOverlay != null) {
                nowMyPolylineOverlay.removeThis();
            }
        }
                /*if (type.equals("POLYGON")) {
                    if (nowMyPolyGoneOverlay != null) {
                        *//*mapView.getOverlays().remove(nowMyPolyGoneOverlay);
                        nowMyPolyGoneOverlay= null;*//*
                        nowMyPolyGoneOverlay.removeThis();
                    }
                }*/
    }

    /**
     * 设置当前的图层
     * 用于结束画线和空域，同样图层换了
     */
            /*public void setNowMyPolylineOverlay(MyPolylineOverlay nowMyPolylineOverlay){
                this.nowMyPolylineOverlay = nowMyPolylineOverlay;
            }
            public void setNowMyPolyGoneOverlay(MyPolyGoneOverlay nowMyPolyGoneOverlay){
                this.nowMyPolyGoneOverlay = nowMyPolyGoneOverlay;
            }*/
    public void setNowMyPolylineOverlay(RouteLay nowMyPolylineOverlay) {
        MyOverlayManager.nowMyPolylineOverlay = nowMyPolylineOverlay;
    }

            /*public void setNowMyPolyGoneOverlay(AirSpaceLay nowMyPolyGoneOverlay) {
                this.nowMyPolyGoneOverlay = nowMyPolyGoneOverlay;
            }*/

    /**
     * getter和setter
     */
    public static HashMap<String, Overlay> getAllmyoverlaysHash_Overlay() {
        return allmyoverlaysHash_Overlay;
    }

    public static void setAllmyoverlaysHash_Overlay(HashMap<String, Overlay> allmyoverlaysHash_Overlay) {
        MyOverlayManager.allmyoverlaysHash_Overlay = allmyoverlaysHash_Overlay;
    }

            /*public static HashMap<String, MyBasicEntity> getAllmyoverlaysHash_Entity() {
                return allmyoverlaysHash_Entity;
            }

            public static void setAllmyoverlaysHash_Entity(HashMap<String, MyBasicEntity> allmyoverlaysHash_Entity) {
                MyOverlayManager.allmyoverlaysHash_Entity = allmyoverlaysHash_Entity;
            }

            public static HashMap<String, Overlay> getAllMyMarkerOverlay() {
                return allMyMarkerOverlay;
            }

            public static void setAllMyMarkerOverlay(HashMap<String, Overlay> allMyMarkerOverlay) {
                MyOverlayManager.allMyMarkerOverlay = allMyMarkerOverlay;
            }

            public static HashMap<String, Overlay> getAllMyPolyGonOverlay() {
                return allMyPolyGonOverlay;
            }

            public static void setAllMyPolyGonOverlay(HashMap<String, Overlay> allMyPolyGonOverlay) {
                MyOverlayManager.allMyPolyGonOverlay = allMyPolyGonOverlay;
            }*/

    public static HashMap<String, Overlay> getAllMyPolyLineOverlay() {
        return allMyPolyLineOverlay;
    }

    public static void setAllMyPolyLineOverlay(HashMap<String, Overlay> allMyPolyLineOverlay) {
        MyOverlayManager.allMyPolyLineOverlay = allMyPolyLineOverlay;
    }

            /*public static AirSpaceLay getNowMyPolyGoneOverlay() {
                return nowMyPolyGoneOverlay;
            }*/

    //
    public void setOvery_Info() {
        //Overlay_info.setVisibility(View.VISIBLE);
        Overlay_info.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //int i = MyOverlayManager.allmyoverlaysHash_Entity.size();
                final int j = MyOverlayManager.allmyoverlaysHash_Overlay.size();
                //int k = MyOverlayManager.allMyMarkerOverlay.size();
                final int m = MyOverlayManager.allMyPolyLineOverlay.size();
                //int n = MyOverlayManager.allMyPolyGonOverlay.size();
                Overlay_info.post(new Runnable() {
                    @Override
                    public void run() {
                        Overlay_info.setText("allOverlay:" + j + "\n" +
                                "allMyPolyLineOverlay:" + m + "\n");

                    }
                });
            }
        });
    }

    public void closeOvery_Info() {
        Overlay_info.setVisibility(View.GONE);
    }

    public RouteLay getPolylineKeyUK(String markerUK) {
        System.out.println("通过Map.entrySet遍历key和value");
        for (String key : allMyPolyLineOverlay.keySet()) {
            if (((RouteLay) allMyPolyLineOverlay.get(key)).getWhickMarkerUK().equals(markerUK)) {
                return (RouteLay) allMyPolyLineOverlay.get(key);
            }
            System.out.println("key= " + key + " and value= " + allMyPolyLineOverlay.get(key));
        }
        return null;
    }

}
